﻿
@{
    ViewData["Title"] = "FinishedUpdate";
    Layout = "~/Views/Home/Menu.cshtml";
}

<h1>成品修改</h1>

<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">成品编号</label>
            <div class="layui-input-inline">
                <input type="text" name="finishedProductNumbering" id="finishedProductNumbering" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">成品名称</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="finishedProductName" id="finishedProductName" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">规格型号</label>
            <div class="layui-input-inline">
                <input type="text" name="finishedProductModel" id="finishedProductModel" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">单位</label>
            <div class="layui-input-inline layui-input-wrap">
                <select name="finishedProductUnit" id="finishedProductUnit" lay-filter="aihao">
                    <option value="">请选择</option>
                    <option value="个">个</option>
                    <option value="箱">箱</option>
                    <option value="件">件</option>
                    <option value="套">套</option>
                    <option value="台">台</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">库存上限</label>
        <div class="layui-input-inline">
            <input type="text" name="finishedProductUpperLimit" id="finishedProductUpperLimit" autocomplete="off" class="layui-input">
        </div>

        <label class="layui-form-label">报警天数</label>
        提前<div class="layui-inline">
            <div class="layui-input-inline layui-input-wrap" style="width: 100px;">
                <input type="number" name="finishedProductDays" id="finishedProductDays" autocomplete="off" class="layui-input" min="0" step="1" lay-affix="number">
            </div>
        </div>天报警
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">批次号</label>
            <div class="layui-input-inline">
                <input type="text" name="finishedProductBatch" id="finishedProductBatch" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">库存下限</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" name="finishedProductLowerLimit" id="finishedProductLowerLimit" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="finishedProductState" id="finishedProductState" value="1" title="启用" checked>
            <input type="radio" name="finishedProductState" id="finishedProductState" value="2" title="禁用">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="finishedProductRemark" id="finishedProductRemark" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit lay-filter="finisUpdate">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>



<script>
    //文档就绪函数
    $(function () {
        fantian()
    })
    var obj = {
        finishedProductId: 0,
        finishedProductNumbering: "",
        finishedProductName: "",
        finishedProductModel: "",
        finishedProductUnit: "",
        finishedProductUpperLimit: 0,
        finishedProductDays: 0,
        finishedProductBatch: "",
        finishedProductLowerLimit: 0,
        finishedProductState: 0,
        finishedProductRemark: "",
        finishedProductImg: "",
        addName: "",
        addTime: "",
    }
    function fantian(){
        return new Promise(function (resolve, reject) {
            $(document).ready(function () {
                // 获取要编辑的用户ID，可以通过URL参数或者其他方式获取，这里假设通过URL参数获取
                var finishedProductId = localStorage.getItem("finishedProductId");
                console.log(finishedProductId);
                // 如果获取到了用户ID，就去后台获取该用户的详细信息并反填到表单
                if (finishedProductId) {
                    $.ajax({
                        type: "get",
                        url: "/FinishedProduct/FinishedProductAddMondel",
                        data: {
                            id: finishedProductId,
                            token: localStorage.getItem("token")
                        },
                        success: function (res) {
                            console.log(res)
                            var rest = JSON.parse(res)
                            console.log(rest)
                            if (rest.data) {
                                var rest=rest.data
                                $('#finishedProductId').val(rest.finishedProductId);
                                $('#finishedProductName').val(rest.finishedProductName);
                                $('#finishedProductNumbering').val(rest.finishedProductNumbering);
                                $('#finishedProductModel').val(rest.finishedProductModel);
                                $('#finishedProductUnit').val(rest.finishedProductUnit);
                                layui.form.render('select')
                                $('#finishedProductUpperLimit').val(rest.finishedProductUpperLimit);
                                $('#finishedProductDays').val(rest.finishedProductDays);
                                $('#finishedProductBatch').val(rest.finishedProductBatch);
                                $('#finishedProductLowerLimit').val(rest.finishedProductLowerLimit);
                                $('#finishedProductState').val(rest.finishedProductState);
                                //是否禁用
                                var lswarehouseState = rest.finishedProductState
                                $("[name='finishedProductState']").each(function () {
                                    if (lswarehouseState == $(this).val()) {
                                        $(this).prop("checked", true);
                                    }
                                })
                                $('#finishedProductRemark').val(rest.finishedProductRemark);
                                $('#finishedProductImg').val(rest.finishedProductImg);
                                $('#addName').val(rest.addName);
                                $('#addTime').val(rest.addTime);

                                obj.finishedProductId = rest.finishedProductId,
                                    obj.finishedProductNumbering = rest.finishedProductNumbering,
                                    obj.finishedProductName = rest.finishedProductName,
                                    obj.finishedProductModel = rest.finishedProductModel,
                                    obj.finishedProductUnit = rest.finishedProductUnit,
                                    obj.finishedProductUpperLimit = rest.finishedProductUpperLimit,
                                    obj.finishedProductDays = rest.finishedProductDays,
                                    obj.finishedProductBatch = rest.finishedProductBatch,
                                    obj.finishedProductLowerLimit = rest.finishedProductLowerLimit,
                                    obj.finishedProductState = rest.finishedProductState,
                                    obj.finishedProductRemark = rest.finishedProductRemark,
                                    obj.finishedProductImg = rest.finishedProductImg,
                                    obj.addName = rest.addName, 
                                    obj.addTime = rest.addTime
                            }
                        }
                    })
                }
            });
        });
    }
    var obj1 = {
        finishedProductId: 0,
        finishedProductNumbering: "",
        finishedProductName: "",
        finishedProductModel: "",
        finishedProductUnit: "",
        finishedProductUpperLimit: 0,
        finishedProductDays: 0,
        finishedProductBatch: "",
        finishedProductLowerLimit: 0,
        finishedProductState: 0,
        finishedProductRemark: "",
        finishedProductImg: "",
        addName: "",
        addTime: "",
        updateName:"张三",
        updateTime:""
    }
    layui.use(['layer', 'form', 'util'], function () {
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var util = layui.util;

        form.on('submit(finisUpdate)', function (data) {
            var field = data.field; //获取表单字段值
            obj1.finishedProductId = obj.finishedProductId,
                obj1.finishedProductNumbering = field.finishedProductNumbering,
                obj1.finishedProductName = field.finishedProductName,
                obj1.finishedProductModel = field.finishedProductModel,
                obj1.finishedProductUnit = field.finishedProductUnit,
                obj1.finishedProductUpperLimit = field.finishedProductUpperLimit,
                obj1.finishedProductDays = field.finishedProductDays,
                obj1.finishedProductBatch = field.finishedProductBatch,
                obj1.finishedProductLowerLimit = field.finishedProductLowerLimit,
                obj1.finishedProductState = $('#finishedProductState').is(':checked ') ? "1" : "2",
                obj1.finishedProductRemark = field.finishedProductRemark,
                obj1.finishedProductImg = field.finishedProductImg,
                obj1.addName = obj.addName,
                obj1.addTime = obj.addTime
            obj1.updateName = "张三"
                console.log(obj1)
            // 显示填写结果，仅作演示用
            // 此处可执行 Ajax 等操作
            debugger
            $.ajax({
                url: '/FinishedProduct/FinishedProductUpdate',
                type: 'post',
                data: { dto: obj1, token: localStorage.getItem("token") },
                success: function (res) {
                    var res = JSON.parse(res)
                    console.log(obj1);
                    console.log(res);
                    if (res.data == 1) {
                        layer.msg("修改成功", { icon: 1 })
                        setTimeout(function () {
                            location.href = '/FinishedProduct/FinishedShow'
                        }, 2000);
                    } else {
                        layer.msg("修改失败:" + res.message, { icon: 2 });
                    }
                }
            });

            return false; //阻止默认 form 跳转
        });
        // 对弹层中的表单进行初始化渲染
        form.render();
    });
</script>